import React from 'react'
import { changeNav } from '../../../actions'
import { connect } from 'react-redux'
import {
    Button, List, InputItem, Flex, WingBlank, WhiteSpace, Popup, Tag, Toast, Switch
} from 'antd-mobile'
import { createForm } from 'rc-form';
import hhistory from '../../../history'
import { ajaxGet, ajaxPost } from 'wxAjaxUtil';
import styles from '../wxelm/mdConfig.less';

const Item = List.Item;
const Brief = Item.Brief;


class WmConfig extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            res: {}
        };
        this.submit=this.submit.bind(this);
    }
    
    componentDidMount() {
        this.props.tochangeNav({
            title: "餐道外卖集团配置", rbtn: [
                <Button onClick={this.submit} key="1" type="primary" inline size="small">确定</Button>
            ]
        });
        
        var postData=this.props.location.state.groupCode;
        console.log(postData);
        //请求数据
        ajaxPost("/o2o/sly/conf/load/cdwm/group", postData, 'wxApiPath').then(res => {
            if (res.returnCode && res.returnCode == "1") {
                this.setState({
                    res: res.data,
                });
                this.state.res.groupInfo=postData.groupCode;
            } else {
                Toast.fail('加载饿了吗外卖配置失败', 1);
            }
        });
        
    }

    submit() {
        const subData = {
            "groupInfo":
            [
                this.state.res.groupInfo
            ],
            "accesskey":this.state.res.accesskey,
            "secret":this.state.res.secret,
            "key":this.state.res.key
        };
        ajaxPost("/o2o/sly/conf/apply/cdwm/group", subData, 'wxApiPath').then(res => {
            if (res.returnCode && res.returnCode == "1") {
                Toast.success('应用饿了吗外卖集团配置成功', 1);
            } else {
                Toast.fail('应用饿了吗外卖集团配置失败', 1);
            }
        });
        var backRouter = this.props.location.state.backRouter;
        hhistory.push({
			pathname: backRouter
		});

    }

    render() {
        return (
            <div>
                <List renderHeader={() => '餐道密匙'}>
                    <Item><div><span>accesskey</span><input onBlur={(e)=>{this.state.res.accesskey=e.target.value}} className={styles.input} type="text" placeholder={this.state.res.accesskey ? this.state.res.accesskey : "必填"} /></div></Item>
                    <Item><div><span>Sercet</span><input onBlur={(e)=>{this.state.res.secret=e.target.value}} className={styles.input} type="text" placeholder={this.state.res.secret ? this.state.res.secret : "必填"} /></div></Item>
                    <Item><div><span>Key</span><input onBlur={(e)=>{this.state.res.key=e.target.value}} className={styles.input} type="text" placeholder={this.state.res.key ? this.state.res.key : "必填"} /></div></Item>
                </List>
            </div>
        )
    }


}

const mapStateToProps = (state) => ({
    wmConfigData: state.wxElms.wmConfigData
})
const mapDispatchToProps = (dispatch) => ({
    tochangeNav: navogj => dispatch(changeNav(navogj)),
})

export default connect(mapStateToProps, mapDispatchToProps)(WmConfig)